//创建用户相关的小仓库
import { defineStore } from "pinia";
//引入接口
import { getRoleList, insertRole, updateRole } from "@/api/role";
//引入数据类型

import type { RoleState } from "./types/type";
import type { addRoleResponseData, getRoleListResponseData, roleInfoData } from "@/api/role/type";
//创建用户小仓库
//User是当前小仓库的唯一id
let useRoleStore = defineStore("Role",{
    
    //之类使用选择是Api写法
    //存储数据位置
    state:():RoleState=>{
        return{
            roles:[]
        }
    },
    //异步逻辑
    actions:{
        async roleList(){
            let result:getRoleListResponseData = await getRoleList();
            if(result.status == 0){
                this.roles = result.data
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async addRole(data:roleInfoData){
            let result:addRoleResponseData = await insertRole(data);
            if(result.status == 0){
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 设置角色权限
        async updateRole(data:roleInfoData){
            const result:addRoleResponseData = await updateRole(data);
            console.log(result);
            
            if(result.status == 0){
                return "OK"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        }
    }
})

//把小仓库暴露出去
export default useRoleStore;